package com.cgtong.android.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cgtong.android.R;
import com.cgtong.android.bean.ProblemBean;
import com.chad.library.adapter.base.BaseViewHolder;
import com.github.vipulasri.timelineview.TimelineView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 步骤进度适适配器
 * Created by chunc on 2017/10/19.
 */

public class HorizontalStepAdapter extends RecyclerView.Adapter<HorizontalStepAdapter.StepViewHolder> {
    private Context mContext;
    private LayoutInflater layoutInflater;

    private String[] stepArray;
    private int status;
    private int isExpired;
    private List<ProblemBean.StatusChangeBean> statusChangeBeanList;

    public HorizontalStepAdapter(Context context, int status, int isExpired, List<ProblemBean.StatusChangeBean> statusChangeBeanList) {
        this.mContext = context;
        this.layoutInflater = LayoutInflater.from(mContext);
        this.status = status;
        this.isExpired = isExpired;
        this.statusChangeBeanList = statusChangeBeanList;
        this.stepArray = mContext.getResources().getStringArray(R.array.vertical_step_array);
    }

    @Override
    public int getItemViewType(int position) {
        return TimelineView.getTimeLineViewType(position, getItemCount());
    }

    @Override
    public StepViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.layout_horizontal_step, parent, false);
        return new StepViewHolder(view, viewType);
    }

    @Override
    public void onBindViewHolder(StepViewHolder holder, int position) {
        holder.msgTxt.setText(stepArray[position]);
        /*
         * 已上报:  上传到系统未到达网格   绿实 status:7
         * 已受理:  上传到网格未受理      绿空 status:1(desc为空，文字灰色，不为空，显示绿色)
         *         上传到网格已受理      绿实 status:2
         * 处理中:  处置未超期           绿空 status:3 isExpired:2
         *         处置超期             红空 status:3 isExpired:1
         *         处置未超期结案        绿实 status:4 isExpired:2
         *         处置超期结案          红实 status:4 isExpired:1

         * 已结案:  未超期结案           绿实 status:4 isExpired:2
         *         超期结案             红实 status:4 isExpired:1
         *         已评价且未超期        绿实 status:5 isExpired:2
         *         已评价且超期          红实 status:5 isExpired:1
         *  已作废： status:6   全部灰实
         */

        if (status != 6) {
            if (position == 0) {
                //已上报
                holder.timelineView.setMarker(ContextCompat.getDrawable(mContext, R.drawable.ic_attention_dot));
                holder.msgTxt.setTextColor(ContextCompat.getColor(mContext, R.color.color_order_complete));
            } else if (position == 1) {
                //已受理
                if (status == 1) {
                    holder.timelineView.setMarker(ContextCompat.getDrawable(mContext, R.drawable.ic_hollow_dot_green));
                    //判断【已受理】字体颜色是绿色还是灰色
                    boolean descIsNull = true;
                    if (statusChangeBeanList != null && statusChangeBeanList.size() >= 2) {
                        ProblemBean.StatusChangeBean bean = statusChangeBeanList.get(1);
                        if (!TextUtils.isEmpty(bean.getDesc())) {
                            descIsNull = false;
                        }
                    }
                    if (descIsNull) {
                        holder.msgTxt.setTextColor(ContextCompat.getColor(mContext, R.color.color_order_incompleted));
                    } else {
                        holder.msgTxt.setTextColor(ContextCompat.getColor(mContext, R.color.color_order_complete));
                    }
                } else if (status == 2 || status == 3 || status == 4 || status == 5) {
                    holder.timelineView.setMarker(ContextCompat.getDrawable(mContext, R.drawable.ic_attention_dot));
                    holder.msgTxt.setTextColor(ContextCompat.getColor(mContext, R.color.color_order_complete));
                } else if (status == 7) {
                    holder.timelineView.setMarker(ContextCompat.getDrawable(mContext, R.drawable.ic_default_dot_hollow));
                    holder.msgTxt.setTextColor(ContextCompat.getColor(mContext, R.color.color_order_incompleted));
                }
            } else if (position == 2) {
                //处理中
                if (status == 3 && isExpired == 2) {
                    holder.timelineView.setMarker(ContextCompat.getDrawable(mContext, R.drawable.ic_hollow_dot_green));
                    //判断【处理中】字体颜色是绿色还是灰色
                    boolean descIsNull = true;
                    if (statusChangeBeanList != null && statusChangeBeanList.size() >= 3) {
                        ProblemBean.StatusChangeBean bean = statusChangeBeanList.get(2);
                        if (!TextUtils.isEmpty(bean.getDesc())) {
                            descIsNull = false;
                        }
                    }
                    if (descIsNull) {
                        holder.msgTxt.setTextColor(ContextCompat.getColor(mContext, R.color.color_order_incompleted));
                    } else {
                        holder.msgTxt.setTextColor(ContextCompat.getColor(mContext, R.color.color_order_complete));
                    }
                } else if (status == 3 && isExpired == 1) {
                    holder.timelineView.setMarker(ContextCompat.getDrawable(mContext, R.drawable.ic_hollow_dot_red));
                    holder.msgTxt.setTextColor(ContextCompat.getColor(mContext, R.color.red));
                } else if ((status == 4 && isExpired == 2) || (status == 5)) {
                    holder.timelineView.setMarker(ContextCompat.getDrawable(mContext, R.drawable.ic_attention_dot));
                    holder.msgTxt.setTextColor(ContextCompat.getColor(mContext, R.color.color_order_complete));
                } else if (status == 4 && isExpired == 1) {
                    holder.timelineView.setMarker(ContextCompat.getDrawable(mContext, R.drawable.ic_solid_dot_red));
                    holder.msgTxt.setTextColor(ContextCompat.getColor(mContext, R.color.red));
                } else if (status == 7 || status == 1 || status == 2) {
                    holder.timelineView.setMarker(ContextCompat.getDrawable(mContext, R.drawable.ic_default_dot_hollow));
                    holder.msgTxt.setTextColor(ContextCompat.getColor(mContext, R.color.color_order_incompleted));
                }
            } else if (position == 3) {
                //已结案
                if ((status == 4 || status == 5) && isExpired == 2) {
                    holder.timelineView.setMarker(ContextCompat.getDrawable(mContext, R.drawable.ic_attention_dot));
                    holder.msgTxt.setTextColor(ContextCompat.getColor(mContext, R.color.color_order_complete));
                } else if ((status == 4 || status == 5) && isExpired == 1) {
                    holder.timelineView.setMarker(ContextCompat.getDrawable(mContext, R.drawable.ic_solid_dot_red));
                    holder.msgTxt.setTextColor(ContextCompat.getColor(mContext, R.color.red));
                } else if (status == 7 || status == 1 || status == 2 || status == 3) {
                    holder.timelineView.setMarker(ContextCompat.getDrawable(mContext, R.drawable.ic_default_dot_hollow));
                    holder.msgTxt.setTextColor(ContextCompat.getColor(mContext, R.color.color_order_incompleted));
                }
            }
        }
    }

    @Override
    public int getItemCount() {
        return 4;
    }


    class StepViewHolder extends BaseViewHolder {

        @BindView(R.id.date_txt)
        TextView dateTxt;
        @BindView(R.id.msg_txt)
        TextView msgTxt;
        @BindView(R.id.time_marker)
        TimelineView timelineView;

        public StepViewHolder(View itemView, int viewType) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            timelineView.initLine(viewType);
        }
    }
}
